package com.cyf.service.impl;

import com.cyf.dao.AccountDao;
import com.cyf.service.AccountService;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author cyf
 * @description
 * @create 2018-09-14 19:50
 **/
public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;

    private DataSource dataSource;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update() throws Exception {
        Connection connection = DataSourceUtils.getConnection(dataSource);
        try {
            Statement statement = connection.createStatement();
            statement.executeUpdate("update users u set u.user_name = 'update3' where u.id = 1");
//            int a = 5 /0;
//            throw new RuntimeException("更新出现异常!");
            statement.executeUpdate("update users u set u.user_name = 'update4' where u.id = 3");
        } catch (SQLException e) {
            throw new Exception();
//            e.printStackTrace();
        } finally {
            DataSourceUtils.releaseConnection(connection,dataSource);
        }
    }
}
